package cn.com.connected.controller.open;

import cn.com.connected.base.cache.CacheService;
import cn.com.connected.common.emnus.CacheKey;
import cn.com.connected.common.emnus.HttpParams;
import cn.com.connected.common.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@RestController
@Api(value = "图片验证码")
@Slf4j
public class IdentifyingController {

    @Resource
    CacheService cacheService;

    @ApiOperation(value="描述:获取验证码 ok")
    @RequestMapping(value="/api/v1/identifying",method= RequestMethod.GET)
    public void identifying(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        String uu1 = request.getParameter(HttpParams.UU1);

        log.info("uu1 >>>>>>>> {}",uu1);

        cacheService.putObject(String.format(CacheKey.IMAGE_CODE, uu1),verifyCode,200);

        // 生成图片
        int w = 100, h = 30;
        OutputStream out = response.getOutputStream();
        VerifyCodeUtils.outputImage(w, h, out, verifyCode);
    }

}
